package com.myTimeUtils.android.TimeTracker.Schedule;
import com.myTimeUtils.android.TimeTracker.Global.Iterator;
/**
 * Models a schedule and constructs an iterator over that schedule.
 *
 * @author Joseph Walker
 */
public class Schedule{
	/**
	 * Schedule representation of Monday.
	 */
	public final static int MONDAY = 0;
	/**
	 * Schedule representation of Friday.
	 */
	public final static int FRIDAY = 4;
	/**
	 * Schedule representation of Saturday.
	 */
	public final static int SATURDAY = 5;
	/**
	 * Schedule representation of Sunday.
	 */
	public final static int SUNDAY = 6;
	/**
	 * Schedule representation of Thursday.
	 */
	public final static int THURSDAY = 3;
	/**
	 * Schedule representation of Tuesday.
	 */
	public final static int TUESDAY = 1;
	/**
	 * Schedule representation of Wednesday.
	 */
	public final static int WEDNESDAY = 2;
	private Day week[];
	/**
	 * Constructs a schedule object.
	 */	
	public Schedule(){
		this.init();
	}
	/**
	 * Adds the provided period to the given day.
	 *
	 * @param period	The period to add
	 * @param day		The day to add it to
	 * @return		True if the period can be added
	 */
	public boolean addPeriod(Period period, int day){
		if(day < 0 || day >= 7) return false;
		return this.week[day].addPeriod(period);
	}
	/**
	 * Constructs an iterator over the periods in this schedule.
	 *
	 * @return		An iterator over the periods
	 */
	public Iterator<Period> getIterator(){
		return new Iterator<Period>(){
			private Day schedule[];
			private Iterator<Period> currentDayIter;
			private int currentDayNum;
			{
				this.schedule = week;
				this.clear();
			}
			public void clear(){ 
				this.currentDayNum = 0; 
				this.currentDayIter = this.schedule[this.currentDayNum].getIterator();
				while(!this.currentDayIter.hasNext() && this.currentDayNum < 6){
					this.currentDayNum++;
					this.currentDayIter = this.schedule[this.currentDayNum].getIterator();
				}
			}
			public Period current(){ return this.currentDayIter.current(); }
			public boolean hasNext(){ return this.currentDayNum < 7 && this.currentDayIter.hasNext(); }
			public void next(){
				this.currentDayIter.next(); 		
				while(!this.currentDayIter.hasNext() && this.currentDayNum < 6){
					this.currentDayNum++;
					this.currentDayIter = this.schedule[this.currentDayNum].getIterator();
				}			
			}
		};		
	}
	/**
	 * Returns the number of periods in the specified day.
	 *
	 * @param day		The day to retrieve the number of periods form
	 * @return		The number of periods in the specified day
	 */
	public int getNumPeriods(int day){
		return this.week[day].getNumPeriods();
	}
	/**
	 * Returns the specified period from the specified day.
	 *
	 * @param day		The day number to return period from
	 * @param period		The period to return from the day
	 * @return		The specified day
	 */
	public Period getPeriod(int day, int period){
		return this.week[day].getPeriod(period);
	}
	private void init(){
		this.week = new Day[7];
		for(int x = 0; x < 7; x++){
			this.week[x] = new Day();
		}
	}
	/**
	 * Removes a period from the given day.
	 *
	 * @param period	The period to remove
	 * @param day		The day to remove from
	 * @return 		True if the period can be removed
	 */
	public boolean removePeriod(Period period, int day){
		if(day < 0 || day >= 7) return false;
		return this.week[day].removePeriod(period);
	}
}
